//
//  MaskRmbgViewController.swift
//  Matting
//  Created by JoyWT on 2022/3/10
//  
    

import UIKit
import opencv2
class MaskRmbgViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func pickedDone() {
        
        let md = Mat(uiImage: maskImage!) //deaplab生成的mask图 ,黑色为前景区域
        //对mask区域进行凸起平滑处理,仅对车辆有效,其他有毛边的不适用
//        let md1 = Mat()
//        Imgproc.medianBlur(src: md, dst: md1, ksize: 41)
        //对mask区域进行凸起平滑处理,仅对车辆有效,其他有毛边的不适用
        //处理背景图片
        let nmd = Mat()
        Core.bitwise_not(src: md, dst: nmd) //对mask图片取反 白色为前景区域
        
//        //获取前景图像(抠图的部分)
//        let nd = Mat()
//        od.copy(to: nd, mask: md1)
//        let img = removeBg(origin: originalImage!, alpha: nmd.toUIImage())
//        let endImg = Mat()
//        Imgproc.GaussianBlur(src: Mat(uiImage: img), dst: endImg, ksize: Size2i(width: 9, height: 9), sigmaX: 5)
        let m = Mat(uiImage: UIImage(named: "333.png")!)
        print(md.channels())
        print(md.type())
        print(m.channels())
        print(m.type())

//        imageView.image = maskImageView(originalImage!, mask: UIImage(named: "333.png")!)
        imageView.image = maskImageView(originalImage!, mask: md.toUIImage())

//        imageView.image = maskImage

    }
    func removeBg(origin:UIImage , alpha: UIImage)-> UIImage{
        let od = Mat(uiImage: origin)
        let md = Mat(uiImage: alpha)
        let cd = Mat(rows: od.rows(), cols: od.cols(), type: od.type(), scalar: Scalar(0))
        let cdp = cd.dataPointer()
        let odp = od.dataPointer()
        let mdp = md.dataPointer()

        let rgb = od.channels()
        for i in 0...od.total(){
            if mdp[Int(i)] > 128 {
                for wi in 0...Int(rgb) {
                    let index = i*Int(rgb) + wi
                    cdp[index] =  odp[index]
                }
            }
        }

        return cd.toUIImage()
    }
    
    func maskImageView(_ orgin: UIImage, mask: UIImage) -> UIImage? {
            guard let source = orgin.gaussianFilter(10)
            else {
                return nil
            }
            let size = orgin.size
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            orgin.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
            mask.draw(in: CGRect(origin: .zero, size: size), blendMode: .luminosity, alpha: 0)
//            mask.draw(in: CGRect(origin: .zero, size: size))
            let result = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            UIGraphicsBeginImageContextWithOptions(size, true, 0)
            source.draw(in: CGRect(x: -30, y: -30, width: size.width + 60, height: size.height + 60))
            result?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
            let result1 = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return result1
        }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
